package com.zm.zmgz.utils;

import com.zm.zmgz.pojo.ShowEmployeeBean;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


public class UniversalUtils { // 通用工具类

    public static List<String> getAttributes(Class clazz) { // 传入class,按顺序返回属性名list
        Field[] fields = clazz.getDeclaredFields();
        List<String> fieldslist = new ArrayList<>();
        for (Field field : fields) {
            String[] splits = field.toGenericString().split("\\."); // 使用反斜杠转义
            fieldslist.add(splits[splits.length-1]);
        }
        return fieldslist;
    }


    public static Object getFieldValueByFieldName(String fieldName,Object object) { // 根据属性名返回属性值
        try {
//            System.out.println("获取["+fieldName+"]属性的值");
            Field field = object.getClass().getDeclaredField(fieldName);
            field.setAccessible(true); // 对private的属性的访问
            return field.get(object);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
